package com.sunny.shop.service;

import com.sunny.shop.bo.IntegralLogBO;
import com.sunny.shop.core.LocalUser;
import com.sunny.shop.dto.SignDTO;
import com.sunny.shop.exception.httpe.ForbiddenException;
import com.sunny.shop.model.IntegralLog;
import com.sunny.shop.model.Sign;
import com.sunny.shop.model.User;
import com.sunny.shop.repository.IntegralLogRepository;
import com.sunny.shop.repository.SignRepository;
import com.sunny.shop.repository.UserRepository;
import com.sunny.shop.vo.SignVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

@Service
public class SignServiceImpl {
    @Autowired
    private SignRepository signRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private IntegralLogRepository integralLogRepository;

    public Integer sign(){
        Long uid = LocalUser.getUser().getId();
        // 今日是否已签到
        String today = SignServiceImpl._getFirstDay(true);
        Sign res = signRepository.findToday(uid, today);
        if(res != null) throw new ForbiddenException(60000);
        // 昨日是否签到
        String lastDay = SignServiceImpl._getFirstDay(false);
        Sign lastRes = signRepository.findToday(uid, lastDay);
        Integer countDay = 1;
        Integer integral = 10;
        if(lastRes != null && lastRes.getDays() < 7){
            countDay = lastRes.getDays();
            integral = SignServiceImpl._getIntegral(++countDay);
        }
        SignDTO build = SignDTO.builder()
                .uid(uid)
                .signTime(today)
                .days(countDay)
                .integral(integral)
                .build();
        Sign sign = new Sign();
        BeanUtils.copyProperties(build,sign);
        signRepository.save(sign);
        User user = userRepository.findOneById(uid);
        int orgIntegral = user.getIntegral() != null ? user.getIntegral().intValue() : 0;
        int beforeIntegral = orgIntegral + (int)integral;
        IntegralLogBO logBO = IntegralLogBO.builder()
                .uid(uid)
                .afterIntegral(user.getIntegral())
                .beforeIntegral(beforeIntegral)
                .integral(integral)
                .type(1)
                .build();
        IntegralLog integralLog = new IntegralLog();
        BeanUtils.copyProperties(logBO,integralLog);
        integralLogRepository.save(integralLog);
        user.setIntegral(beforeIntegral);
        user.setBeforeIntegral(integral);
        userRepository.save(user);
        return integral;
    }

    private static String _getFirstDay(boolean isToday){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if(!isToday) calendar.add(Calendar.DAY_OF_MONTH,-1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(calendar.getTime());
        return format;
    }
    private static String _getFirstDay(Integer day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH,-day);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(calendar.getTime());
        return format;
    }

    private static String _getLastDay(Integer day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH,+day);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(calendar.getTime());
        return format;
    }

    private static Integer _getIntegral(Integer day)
    {
        Integer[] days = new Integer[8];
        days[0] = 0;
        days[1] = 10;
        days[2] = 15;
        days[3] = 20;
        days[4] = 30;
        days[5] = 35;
        days[6] = 40;
        days[7] = 50;
        return days[day];
    }

    public SignVO getSignList() {
        Long uid = LocalUser.getUser().getId();
        User user = userRepository.getOne(uid);
        Long total = signRepository.countAllByUid(uid);
        String today = SignServiceImpl._getFirstDay(true);
        // 查询近两天的签到
        Sign sign = signRepository.findToday(uid, today);
        if(sign == null){
            String lastDay = SignServiceImpl._getFirstDay(false);
            sign = signRepository.findToday(uid, lastDay);
        }
        HashMap<Integer, String> days = new HashMap<>();
        Integer signDays = sign.getDays();
        String[] dateTime = new String[3];
        if(signDays == 0){
            for (Integer i = 1; i <= 7; i++) {
                dateTime = SignServiceImpl._getLastDay(i).split("-");
                days.put(i,dateTime[1]+"-"+dateTime[2]);
            }
        }else{
            int k = 1;
            for (Integer i = 1; i <= 7; i++) {
                if(i <= signDays){
                    dateTime = SignServiceImpl._getFirstDay(signDays-i).split("-");
                }else{
                    dateTime = SignServiceImpl._getLastDay(k++).split("-");
                }
                days.put(i,dateTime[1]+"-"+dateTime[2]);
            }
        }

        SignVO vo = SignVO.builder()
                        .days(signDays)
                        .list(days)
                        .integral(user.getIntegral())
                        .total(total.intValue())
                        .build();
        return vo;
    }
}
